package com.example.sunmiv1.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.LoginEntity;
import com.example.sunmiv1.entity.NewGoodsListBean;
import com.example.sunmiv1.entity.SellcountBean;
import com.example.sunmiv1.room.entity.MerchandisePrintEntity;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.views.CircleImageView;
import com.example.sunmiv1.vm.GoodsMangerViewModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsManageAdapter extends BaseAdapter {

    private List<NewGoodsListBean.DataBean> list;
    private ViewHolder viewHolder;
    private SellcountBean sellcountBean;
    private ACache aCache;
    private List<MerchandisePrintEntity> mPrintList=new ArrayList<>();
    private GoodsMangerViewModel mViewModel;

    public GoodsManageAdapter(List<NewGoodsListBean.DataBean> list,GoodsMangerViewModel viewModel, SellcountBean sellcountBean,ACache aCache) {
        this.list = list;
        mViewModel=viewModel;
        this.sellcountBean=sellcountBean;
        this.aCache=aCache;
    }

    public void setEntityList(List<MerchandisePrintEntity> list){
        mPrintList.clear();
        mPrintList.addAll(list);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_goods_manage, null);
        viewHolder = new ViewHolder(convertView);
        NewGoodsListBean.DataBean bean = list.get(position);
        viewHolder.itemGoodsManageCheck.setChecked(bean.isCheck());
        RequestOptions options = new RequestOptions();
        options.override(300, 300);
        options.placeholder(R.mipmap.error);//佔位圖  .error(R.mipmap.error_food)//如果是錯誤的情況
        Glide.with(parent.getContext())
                .load(bean.getGoods_img())
                .apply(options)
                .into(viewHolder.itemGoodsManageImg);
        viewHolder.itemGoodsManageName.setText(bean.getGoods_name());
        viewHolder.itemGoodsManageFriNumber.setText("貨號 " + ("1".equals(aCache.getAsString("userType"))?bean.getGood_number():bean.getGoods_number()));
        if ("".equals(bean.getThird_goods_code())){
            viewHolder.itemGoodsManageQ3.setVisibility(View.GONE);
        }else {
            viewHolder.itemGoodsManageQ3.setVisibility(View.VISIBLE);
            viewHolder.itemGoodsManageQ3.setText("Q3編碼 "+bean.getThird_goods_code());
        }
        viewHolder.itemGoodsManageCode.setText("條碼 " + bean.getGoods_code());
        viewHolder.itemGoodsManageStock.setText("庫存 " + bean.getGoods_num());
        viewHolder.itemGoodsManagePurPrice.setText("$" + bean.getPurchase_price());
        viewHolder.itemGoodsManageRetailPrice.setText("$" + bean.getRetail_price());
        viewHolder.itemGoodsManageShopRetailPrice.setText("$" + bean.getOnline_price());

        viewHolder.itemGoodsManageVipRetailPrice.setText(TextUtils.isEmpty(bean.getStore_mem_price()) ? "未設置" : "$" + bean.getStore_mem_price());
        viewHolder.itemGoodsManageVipShopPrice.setText(TextUtils.isEmpty(bean.getShop_mem_price()) ? "未設置" : "$" + bean.getShop_mem_price());
        if ("0".equals(bean.getStatus())) {
            viewHolder.itemGoodsManageOffShelf.setText("下架");
        } else if ("1".equals(bean.getStatus())) {
            viewHolder.itemGoodsManageOffShelf.setText("上架");
        }
        final LinearLayout priceLinear = viewHolder.itemGoodsManagePriceLinear;
        viewHolder.itemGoodsManageLinear.setOnClickListener(v -> {
            if (priceLinear.getVisibility() == View.VISIBLE) {
                priceLinear.setVisibility(View.GONE);
            } else {
                priceLinear.setVisibility(View.VISIBLE);
            }
        });
        viewHolder.itemGoodsManageCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                list.get(position).setCheck(isChecked);
                operation.success(0, position);
            }
        });

        if (sellcountBean!=null&&!"1".equals(sellcountBean.getJurisdiction().getEditgoods())&&!"1".equals(sellcountBean.getJurisdiction().getShowpurch_price())) {
            viewHolder.itemGoodsManageUpdate.setVisibility(View.GONE);
        }else {
            if (sellcountBean!=null&&!"1".equals(sellcountBean.getJurisdiction().getEditgoods())) {
                viewHolder.itemGoodsManageUpdate.setVisibility(View.GONE);
                viewHolder.itemGoodsManageOffShelf.setVisibility(View.GONE);
            }else {
                if ("3".equals(aCache.getAsString("userType"))) {
                    LoginEntity loginEntity = GsonUtils.JsonToBean(aCache.getAsString("loginInfo"), LoginEntity.class);
                    if (loginEntity.getData().getAuth().getGoods_manage_edit() != 1) {
                        viewHolder.itemGoodsManageUpdate.setVisibility(View.GONE);
                        viewHolder.itemGoodsManageOffShelf.setVisibility(View.GONE);
                    } else {
                        viewHolder.itemGoodsManageUpdate.setVisibility(View.VISIBLE);
                        viewHolder.itemGoodsManageOffShelf.setVisibility(View.VISIBLE);
                    }
                } else {
                    viewHolder.itemGoodsManageUpdate.setVisibility(View.VISIBLE);
                    viewHolder.itemGoodsManageOffShelf.setVisibility(View.VISIBLE);
                }
            }
        }

        if (!"1".equals(sellcountBean.getJurisdiction().getDelgoods())) {
            viewHolder.itemGoodsManageDelete.setVisibility(View.GONE);
        }else {
            if ("3".equals(aCache.getAsString("userType"))){
                LoginEntity loginEntity= GsonUtils.JsonToBean(aCache.getAsString("loginInfo"), LoginEntity.class);
                if (loginEntity.getData().getAuth().getGoods_manage_del()!=1){
                    viewHolder.itemGoodsManageDelete.setVisibility(View.GONE);
                }else {
                    viewHolder.itemGoodsManageDelete.setVisibility(View.VISIBLE);
                }
            }else {
                viewHolder.itemGoodsManageDelete.setVisibility(View.VISIBLE);
            }
        }

        viewHolder.itemGoodsManageUpdate.setOnClickListener(v -> {
            operation.success(1, position);
        });
        viewHolder.itemGoodsManageOffShelf.setOnClickListener(v -> {
            operation.success(2, position);
        });
        viewHolder.itemGoodsManageDelete.setOnClickListener(v -> {
            operation.success(3, position);
        });
        MerchandisePrintEntity entity=mViewModel.find(bean);
        if(entity==null || entity.isPrint()){
            viewHolder.itemPrint.setText("打印");
        }else{
            viewHolder.itemPrint.setText("不打印");
        }
        viewHolder.itemPrint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mViewModel.addPrint(bean,GoodsManageAdapter.this);
            }
        });
        return convertView;
    }

    public void setOperation(Operation paramOperation) {
        this.operation = paramOperation;
    }

    private Operation operation;

    public interface Operation {
        void success(int type, int index);
    }

    static class ViewHolder {
        @BindView(R.id.itemGoodsManage_check)
        CheckBox itemGoodsManageCheck;
        @BindView(R.id.itemGoodsManage_img)
        CircleImageView itemGoodsManageImg;
        @BindView(R.id.itemGoodsManage_name)
        TextView itemGoodsManageName;
        @BindView(R.id.itemGoodsManage_friNumber)
        TextView itemGoodsManageFriNumber;
        @BindView(R.id.itemGoodsManage_Q3)
        TextView itemGoodsManageQ3;
        @BindView(R.id.itemGoodsManage_code)
        TextView itemGoodsManageCode;
        @BindView(R.id.itemGoodsManage_img1)
        ImageView itemGoodsManageImg1;
        @BindView(R.id.itemGoodsManage_linear)
        LinearLayout itemGoodsManageLinear;
        @BindView(R.id.itemGoodsManage_purPrice)
        TextView itemGoodsManagePurPrice;
        @BindView(R.id.itemGoodsManage_retailPrice)
        TextView itemGoodsManageRetailPrice;
        @BindView(R.id.itemGoodsManage_vipRetailPrice)
        TextView itemGoodsManageVipRetailPrice;
        @BindView(R.id.itemGoodsManage_shopRetailPrice)
        TextView itemGoodsManageShopRetailPrice;
        @BindView(R.id.itemGoodsManage_vipShopPrice)
        TextView itemGoodsManageVipShopPrice;
        @BindView(R.id.itemGoodsManage_priceLinear)
        LinearLayout itemGoodsManagePriceLinear;
        @BindView(R.id.itemGoodsManage_stock)
        TextView itemGoodsManageStock;
        @BindView(R.id.itemGoodsManage_update)
        TextView itemGoodsManageUpdate;
        @BindView(R.id.itemGoodsManage_offShelf)
        TextView itemGoodsManageOffShelf;
        @BindView(R.id.itemGoodsManage_delete)
        TextView itemGoodsManageDelete;
        @BindView(R.id.itemGoodsManage_print)
        TextView itemPrint;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
